package io.metaloom.qdrant.client.json.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import io.metaloom.qdrant.client.http.model.point.VectorData;
import io.metaloom.qdrant.client.http.model.point.VectorDataMap;
import io.metaloom.qdrant.client.http.model.point.VectorDataPlain;

import java.io.IOException;
import java.util.List;
import java.util.Map.Entry;

public class VectorDataSerializer extends JsonSerializer<VectorData> {

	@Override
	public void serialize(VectorData value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
		if (value instanceof VectorDataMap) {
			VectorDataMap vectorData = (VectorDataMap) value;
			gen.writeStartObject();
			for (Entry<String, List<Double>> entry : vectorData.entrySet()) {
				gen.writeArrayFieldStart(entry.getKey());
				for (double f : entry.getValue()) {
					gen.writeNumber(f);
				}
				gen.writeEndArray();
			}
			gen.writeEndObject();
		} else if (value instanceof VectorDataPlain) {
			VectorDataPlain vectorData = (VectorDataPlain) value;
			List<Double> vector = vectorData.getVector();
			gen.writeStartArray();
			for(double f : vector) {
				gen.writeNumber(f);
			}
			gen.writeEndArray();
		} else {
			throw new RuntimeException("Enountered unknown vector data type. Got: " + value.getClass().getSimpleName());
		}

	}

}
